#!/bin/sh
#
# slay 2.0 - kill all processes belonging to the specified user(s).
# originally by Chris Ausbrooks <fish@bucket.ualr.edu> 
# based on kall (a script of unknown origin)
# Heavily rewritten by Pawel Wiecek <coven@debian.org> for Debian
# This is a free software distributed according to terms of GNU GPL 
# (see /usr/share/common-licenses/GPL)

# Revision history:
# 0.99	First attempt.
# 1.0	Added Butthead.
# 1.1	Added retribution.
# 1.2	Added slayee notification.
# 2.0   Completely rewritten
# 2.1	Fix an *ugly* bug that caused slayer to be slain...

USER=`whoami`
SIGNAL=`echo $1 | grep '^\-.*'`
ME=`basename $0`
COOL='0'

# this piece of nested ifs is added for Debian package only
if [ -f /etc/slay_mode ]
then
  if grep -q mean /etc/slay_mode
  then
    MODE='mean'
  fi
  if grep -q nice /etc/slay_mode
  then
    MODE='nice'
  fi
  if [ -z $SLAY_BUTTHEAD ]
  then
    if grep -q butthead /etc/slay_mode
    then
      SLAY_BUTTHEAD='on'
    fi
    if grep -q normal /etc/slay_mode
    then
      SLAY_BUTTHEAD='off'
    fi
  fi
else
  MODE='mean'
  if [ -z $SLAY_BUTTHEAD ]
  then
    SLAY_BUTTHEAD='off'
  fi
fi

# Command line handling.
if [ "$SIGNAL" != "" ]
then
  shift
else
  SIGNAL="-KILL"
fi

if [ "$SIGNAL" != "-clean" ]
then
  SIGSHOW="$SIGNAL"
else
  SIGSHOW="-TERM + -KILL"
fi

# Help for loosers.
if [ "$1" = "" -o "$1" = "--help" ]
then
  echo "usage: $ME [-signal] name [name...]"
  if [ "$SLAY_BUTTHEAD" = "on" ]
  then
    echo "       Like, kills people and stuff."
    echo "       With -clean kicks ass forst and then does real pain."
  else
    echo "       Kills all processes belonging to any of the given names."
    echo "       Use -clean as a signal name to kill with TERM first and then with KILL."
  fi
  exit -1 
fi

# Misuse trap.
if [ "$USER" != "$1" ]
then
  if [ "$USER" != "root" ]
  then
    if [ "$MODE" = "mean" ]
    then
      $0 -KILL $USER
    else
      if [ "$SLAY_BUTTHEAD" = "on" ]
      then
        echo "${ME}: Cut it out."
      else
        echo "${ME}: Only root gets to do that."
      fi
    fi
    exit 2
  fi
fi

# Main body.
while [ "$1" != "" ]
do
  if [ "$1" = "$USER" ]
  then
    if [ "$SLAY_BUTTHEAD" = "on" ]
    then
      echo "${ME}: Beavis, don't make me have to smack you."
    else
      echo "${ME}: Illegal operation."
    fi
  fi
  COOL="1"
  if [ "$SLAY_BUTTHEAD" = "on" ]
  then
    echo "${ME}: $SIGSHOW is kicking $1's butt!"
    echo -e "\\n\\n\\nI'm kicking your butt.\\n\\n\\n" | write $1 2>/dev/null
  else
    echo "${ME}: Sending $SIGSHOW signal to $1's process(es)..."
    echo -e "\\n\\n\\nYour current session has been terminated.\\n\\n\\n" | \
    	write $1 2>/dev/null
  fi
  if [ "$SIGNAL" = "-clean" ]
  then
    su -m $1 -c "kill -TERM -1 2>/dev/null"
    sleep 10
    su -m $1 -c "kill -KILL -1 2>/dev/null"
  else
    su -m $1 -c "kill $SIGNAL -1 2>/dev/null"
  fi
  shift
done

# Error message.
if [ $COOL = "0" ]
then
  if [ "$SLAY_BUTTHEAD" = "on" ]
  then
    echo "${ME}: How old are you, Beavis?"
  else
    echo "${ME}: Nothing done."
  fi
  exit 1
fi

# Non-error message.
if [ $COOL = "1" ]
then
  if [ "$SLAY_BUTTHEAD" = "on" ]
  then
    echo "${ME}: Whoa, I have the power supreme."
  else
    echo "${ME}: Done."
  fi
  exit 0
fi
